package openConn;


import com.alibaba.druid.pool.DruidDataSource;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class DruidUtils {
    private static DruidDataSource dataSource;
    static {
        Properties properties = new Properties();
        try {
            FileInputStream fileInputStream = new FileInputStream("config/shopping.properties");
            properties.load(fileInputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        dataSource = new DruidDataSource();
        dataSource.setDriverClassName(properties.getProperty("driver"));
        dataSource.setUrl(properties.getProperty("url"));
        dataSource.setUsername(properties.getProperty("username"));
        dataSource.setPassword(properties.getProperty("password"));

        dataSource.setMaxActive(Integer.parseInt(properties.getProperty("maxActive")));
        dataSource.setInitialSize(Integer.parseInt(properties.getProperty("initialSize")));
        dataSource.setMaxWait(Long.parseLong(properties.getProperty("maxWait")));
    }
    public static synchronized Connection openConn() throws SQLException {
        if (dataSource != null && !dataSource.isClosed()){
            return dataSource.getConnection();
        }
        return null;
    }
}
